<?php

    function __autoload($class_name) 
    {
        include $class_name . '.php';
    }
    $page = new page("registration");
    
    $content='';    
    if(isset($_SESSION['user']))
    {
        //stranka pro registrovaneho uzivatele
        $content .= "Jste prihlaseni jako  ". $_SESSION['user']->__get('name') . "</br>";
    }
    else
    {    
        if(isset($_POST['login']))
        {
            //uzivatel vyplnil formular 
            //kontrola vstupu
            try
            {
                $error = array(
                    'login' => false,
                    'password' => false,
                    'password_confirm' => false,
                    'name' => false,
                    'email' => false                
                );
                $error_used = false;
                try{
                    checker::checkString($_POST['login']);
                }catch(UnexpectedValueException $e){                    
                    $error['login'] = true;
                    $error_used = true;                    
                }
                try{
                    checker::checkString($_POST['password']);
                }catch(UnexpectedValueException $e){
                    $error['password'] = true;
                    $error_used = true;  
                }
                try{
                    checker::checkString($_POST['password_confirm']);
                }catch(UnexpectedValueException $e){
                    $error['password_confirm'] = true;
                    $error_used = true;  
                }
                try{
                    checker::checkString($_POST['name']);
                }catch(UnexpectedValueException $e){
                    $error['name'] = true;
                    $error_used = true; 
                }
                try{
                    checker::checkEmail($_POST['email']);
                }catch(UnexpectedValueException $e){
                    $error['email'] = true;
                    $error_used = true; 
                }                
                if($error_used)
                    throw new Exception('spatne vyplnena pole ve formulari!');
                if(0 <> strcmp($_POST['password'],$_POST['password_confirm']))
                    throw new Exception('spatne zopakovane heslo!');
            
                
                $conn = new dbConnection();
                $query = sprintf("SELECT * FROM authors WHERE login = '%s'",
                mysql_real_escape_string($_POST['login']));
                $sql_result = $conn->db_query($query);

                if(mysql_num_rows($sql_result) > 0)                    
                    throw new Exception("Uzivatel se zadanym loginem jiz existuje!");
                
                //pridat uzivatele do databaze                
                $query = sprintf("INSERT INTO authors
                    VALUES (null,'%s','%s','%s', '%s', 1, true)",
                        mysql_real_escape_string($_POST[login]),
                        mysql_real_escape_string(md5($_POST["password"])),
                        mysql_real_escape_string($_POST[name]),
                        mysql_real_escape_string($_POST[email]));
                $result = $conn->db_query($query); 
                if($result == false)
                    throw new Exception("Pridani uzivatele se nezdarilo!");
                $user = new user();
                $user->login($_POST['login'],$_POST['password']);
            }
            catch(Exception $e)
            {
                $content .= $e->getMessage();
            }
        }
    }
    if(!isset($_SESSION['user']))
    {        
        $registration_form = new form("registration", 
                "registration.php", 
                "post",
                "Registrace noveho uzivatele: ",
                array(
                    new formText("login",  "Login: "),
                    new formPassword("password", "Heslo: "),
                    new formPassword("password_confirm",  "Heslo znovu: "),
                    new formText("name",  "Vase jmeno: "),
                    new formText("email",  "Vas email: "),
                    new formSubmit("registrovat")        
                ));
        $content .= $registration_form;
    }
    echo $page($content);
?>
